# -*- coding: utf-8 -*-

'''
Created on 17.07.2011

@author: Vlasov A. I. <root@proscript.ru>
'''

#from django.contrib.auth.views import login, logout

from django.contrib import auth
from django.http import HttpResponseRedirect

from forms import AuthForm
from util.template import render_by_request_context

def login(request):
    
    if request.method == 'POST':
        form = AuthForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            user = auth.authenticate(username=data['username'], password=data['password'])
            
            if user is not None and user.is_active:
                auth.login(request, user)
                return HttpResponseRedirect("/")
    else:
        form = AuthForm()   
    return render_by_request_context(request, 'user/auth.html', { 'form': form })
      
def logout(request):
    auth.logout(request)
    # Перенаправление на страницу.
    return HttpResponseRedirect("/") 